<?php
error_reporting(E_ERROR | E_PARSE);
session_start();



if (!isset($_SESSION["attributes"])) {
    header("Location: index.php");
    exit;
}

global $events;

$attributes = $_SESSION["attributes"];
$log = $_SESSION["log"];


// 模拟 10 个阶段
for ($i = 0; $i < 10; $i++) {
    $eligibleEvents = [];
    foreach ($events as $event) {
        if ($event["condition"]($attributes)) {
            $eligibleEvents[] = $event;
        }
    }

    if (!empty($eligibleEvents)) {
        $randomEvent = $eligibleEvents[array_rand($eligibleEvents)];
        $randomEvent["effect"]($attributes);
        $log[] = $randomEvent["description"];
    }
}

$_SESSION["attributes"] = $attributes;
$_SESSION["log"] = $log;
?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>人生模拟结果</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
        }

        ul {
            list-style-type: none;
            padding: 0;
        }

        li {
            margin-bottom: 5px;
        }
    </style>
</head>

<body>
    <h1>人生模拟结果</h1>
    <h2>最终属性</h2>
    <ul>
        <?php foreach ($attributes as $attribute => $value): ?>
            <li><?php echo $attribute; ?>: <?php echo $value; ?></li>
        <?php endforeach; ?>
    </ul>
    <h2>人生经历</h2>
    <ul>
        <?php foreach ($log as $entry): ?>
            <li><?php echo $entry; ?></li>
        <?php endforeach; ?>
    </ul>
    <a href="index.php">重新开始</a>
</body>

</html>